package com.boardour.toupin.push;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;

import androidx.annotation.Nullable;

public class ProjectionUtils {
    public static final int REQUEST_CODE = 0x101;
    private MediaProjectionManager mManager;
    private Activity mActivity;
    private Intent service;

    public void start(Activity activity) {
        this.mActivity = activity;
        mManager = (MediaProjectionManager) activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        Intent screenCaptureIntent = mManager.createScreenCaptureIntent();
        activity.startActivityForResult(screenCaptureIntent, REQUEST_CODE);
    }

    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            // 这里开启服务
            service = new Intent(mActivity, ProjectionService.class);
            service.putExtra("code", resultCode);
            service.putExtra("data", data);
            mActivity.startService(service);
        }
    }

    public void stop() {
        if (service != null) {
            mActivity.stopService(service);
            service = null;
        }
    }
}
